﻿using System.Diagnostics;
using System.Linq;
using Citi.GenericApplications.SchedulerUi.SchedulerWcf;
using Citi.RoutineBase;

namespace Citi.GenericApplications.SchedulerUi
{
    public static class Util
    {
        public static void  ScheduleSuccessorRoutine(string successorRoutineId, ICurrentAction currentAction)
        {
            if (!string.IsNullOrWhiteSpace(successorRoutineId))
            {
                var id = 0;              

                successorRoutineId.Split('|').ToList().ForEach(x =>
                    {
                        if (int.TryParse(x, out id))
                            ScheduleSuccessorRoutine(id, currentAction);
                    });
            }
        }

        public static void ScheduleSuccessorRoutine(int successorRoutineId, ICurrentAction currentAction)
        {
            if (successorRoutineId <= 0) return;

            currentAction.Description = "Scheduling successor routine";

            using (var client = new ScheduleClient())
                client.ExecuteRoutineOnDemandById(successorRoutineId, null);

            currentAction.Description = "Successor routine scheduled. It will be executed as soon as possible";
        }

        public static void KillExcelProcesses()
        {
            var processes = Process.GetProcessesByName("EXCEL");

            foreach (var process in processes)
            {
                try
                {
                    process.Kill();
                }
                catch { }
            }
        }
    }
}
